﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;

namespace Scheduler
{
    public class Scheduler
    {
        private readonly List<Worker> _workers = new List<Worker>();
        public event Func<object> ChipPoint;

        public Worker AddWorker(int st)
        {
            var wk = new Worker(st);
            _workers.Insert(0, wk);
            return wk;
        }

        private void EmitEvent(Context ctx, EventArgs args) => _workers.ForEach(a => a.EmitEvent(ctx, args));
        public Task Execute(Context context)
        {
            return Task.Run(() => DoExecute(context));
        }
        [DebuggerStepThrough]
        private void DoExecute(Context context)
        {

            var tasks = _workers.Where(wk => !wk.SkipThisPass(context)).Select(w => w.Run(context)).ToArray();
            Task.WaitAll(tasks);

           
 
        }
    }
}